package com.lyw.handle;

import com.lyw.constants.GlobalVariables;
import com.lyw.enums.DuplicateEnum;
import com.lyw.factory.TimerTaskFactory;
import com.lyw.utils.CharacterUtil;
import com.lyw.utils.MouseAndKeyEventUtil;
import org.opencv.core.Mat;

import java.awt.*;

public abstract class DataFilteringAdapter {

  public int[] position = {-1, -1};
  protected long start = System.currentTimeMillis();
  protected int[] count = {-1};
  protected boolean[] clearance = {false, false};
  protected int keycode;
  protected boolean flag = false;
  public String position_name;
  protected Mat mat;

  public static void prepareMove(Rectangle rectangle, int[] positions, int height) throws Exception {
    int[] destPosition = {(int) (385 + rectangle.getX()), (int) (430 + rectangle.getY())}, keycodes = {-1, -1}, character = {-1, -1};
    int threshold = 50; // 模糊值
    boolean[] flag = {false, false};
    CharacterUtil.mobileCharacter(positions, destPosition, keycodes);

    while (true) {
      positions = GlobalVariables.POSITIONS;
      int xAbs = Math.abs(positions[0] - destPosition[0]);
      int yAbs = Math.abs(positions[1] - destPosition[1]);
      if (!flag[0] && xAbs <= threshold) {
        MouseAndKeyEventUtil.keyReleased(keycodes[0]);
        flag[0] = true;
      }
      if (!flag[1] && yAbs <= threshold) {
        MouseAndKeyEventUtil.keyReleased(keycodes[1]);
        flag[1] = true;
      }

      if (flag[0] && flag[1]) {
        System.out.println("移动到了目标地方！");
        return;
      }
    }
  }

  public abstract boolean findTheTarget(Robot robot, int[] rolePosition, Mat res, int rows, int cols, int[] doorPosition);

  public abstract boolean findTheTargetDoor(Robot robot, Mat res, int rows, int cols, int[] rolePosition);

  public abstract boolean findTheTargetDoor(Robot robot, Mat res, Mat door, int[] startPosition, int[] positions);

  public abstract void mobileCharacterSearchTarget(Robot robot, int[] key) throws Exception;

  public abstract void mobileCharacterToDoor(Robot robot, int[] key, int[] rolePosition, int[] doorPosition) throws Exception;

  public abstract boolean findTheTarget(int[] rgb, int width, int length, int[] colors);

  public abstract boolean confirmIfTheDoorIsCorrect(int[] positions);

  public abstract void findDoor(Robot robot, Rectangle rectangle, int[] startPosition, TimerTaskFactory taskFactory, int height, int[] positions) throws Exception;

  public abstract void mobileFindDoor(int[] counts, int[] keyCode) throws Exception;

  public abstract void mobileCharacterToCorrectDoor(int[] positions, int[] keycode) throws Exception;

  public static void move(DataFilteringAdapter filter) throws Exception {
    MouseAndKeyEventUtil.doubleClick(filter.keycode);
    System.out.println("移动到门方向！");
  }

  ;

  public abstract boolean whetherClearance(int[] positions);

  public static void released(DataFilteringAdapter filter) throws Exception {
    MouseAndKeyEventUtil.keyReleased(filter.keycode);
  }

  public abstract void mobileCharacterToDoor(Robot robot, Rectangle rectangle, DuplicateEnum duplicateEnum) throws Exception;
}
